package com.rongzhe.house.manager;

import android.text.TextUtils;

import com.rongzhe.house.entity.Dictionary;
import com.rongzhe.house.entity.ResponseEntityVo;
import com.rongzhe.house.internet.DataListener;
import com.rongzhe.house.internet.InternetRequestWorker;
import com.rongzhe.house.internet.ResponseDispatcher;
import com.rongzhe.house.internet.net.DictionaryInterface;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by huang on 2017/12/18.
 */

public class DictionaryManager {
    public static final String DICTIONARY_CONTACT_TIME_OPTION =
            "USER_HOUSE_APPOINTMENT_CONTACT_TIME_OPTION";
    public static final String DICTIONARY_PAY_TYPE_OPTION =
            "House_Rent_Pay_Type";
    private DictionaryManager() {
    }
    private static DictionaryManager sInstance;
    public static DictionaryManager getInstance() {
        if (sInstance == null) {
            sInstance = new DictionaryManager();
        }
        return sInstance;
    }
    private Map<String, List<Dictionary>> mDictionaries = new HashMap<>();

    public void getPareAllItems(String parentCode,
                                            DataListener<List<Dictionary>> listener) {
        List<Dictionary> out =  mDictionaries.get(parentCode);
        if(out != null) {
            listener.onSuccess(out, null);
            return;
        }
        getDictionaryByType(parentCode, listener);
    }

    public String pareItem(String parentCode, String code) {
        List<Dictionary> itemDictionaries = mDictionaries.get(parentCode);
        if (itemDictionaries == null) {
            return null;
        }
        if (TextUtils.isEmpty(code)) {
            return null;
        }
        for (Dictionary dictionary : itemDictionaries) {
            if (code.equals(dictionary.getCode())) {
                return dictionary.getName();
            }
        }
        return null;
    }

    public void getDictionaryByType(final String pareType, DataListener<List<Dictionary>> listener) {
        InternetRequestWorker.getInstance().asyncNetwork(
                InternetRequestWorker.getInstance()
                        .create(DictionaryInterface.class).getDictionaryByCode(pareType),
                new ResponseDispatcher(listener) {
                    @Override
                    public void handleSuccess(ResponseEntityVo data) {
                        if (data.success()) {
                            List<Dictionary> dictionaries = (List<Dictionary>) data.getData();
                            if (dictionaries != null) {
                                List<Dictionary> itemsDictionaries = mDictionaries.get(pareType);
                                if (itemsDictionaries == null) {
                                    itemsDictionaries = new ArrayList<>();
                                    mDictionaries.put(pareType, itemsDictionaries);
                                } else {
                                    itemsDictionaries.addAll(dictionaries);
                                }
                            }
                        }
                    }
                });
    }

    public void getAllDictionary(DataListener<List<Dictionary>> listener) {
        InternetRequestWorker.getInstance().asyncNetwork(
                InternetRequestWorker.getInstance()
                        .create(DictionaryInterface.class).getAllDictionary(),
                new ResponseDispatcher(listener) {
                    @Override
                    public void handleSuccess(ResponseEntityVo data) {
                        if (data.success()) {
                            List<Dictionary> dictionaries = (List<Dictionary>) data.getData();
                            addDictItems(dictionaries);
                        }
                    }
                });
    }

    private void addDictItems(List<Dictionary> dictionaries) {
        for (Dictionary dictionary : dictionaries) {
            List<Dictionary> itemsDictionaries = mDictionaries.get(dictionary.getParentCode());
            if (itemsDictionaries == null) {
                itemsDictionaries = new ArrayList<>();
                mDictionaries.put(dictionary.getParentCode(), itemsDictionaries);
            }

            itemsDictionaries.add(dictionary);
        }
    }
}
